import 'package:easy_refresh/easy_refresh.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:im_app/im/util/DialogUtil.dart';
import 'package:im_app/im/util/UiUtil.dart';

import '../../im/page/bloc/BaseBloc.dart';
import '../../im/page/bloc/DraggableBloc.dart';
import '../../im/page/view/DraggableFloatingView.dart';
import '../../im/pojo/Enums.dart';

final class EasyRefreshCubit extends BaseBloc<EasyRefreshEvent, EasyRefreshState> {

  BuildContext context;
  EasyRefreshCubit(this.context) : super(EasyRefreshState.loading()) {
    on<EasyRefreshInitEvent>((event, emit) {
      _init(state);
    });
    on<EasyRefreshLoadNewsEvent>((event, emit) {
      _loadNews(state, event.refreshController);
    });
    on<EasyRefreshLoadOldsEvent>((event, emit) {
      _loadOlds(state, event.refreshController);
    });
  }

  _init(EasyRefreshState oldState) {
    var dataList = <String>[];
    for (var i=1; i<11; i++) {
      var d = 0 + i;
      dataList.add(d.toString());
    }
    var nextIndex = 0 + 10;
    emit(oldState.copy(nextIndex: nextIndex, preIndex: 10, dataList: dataList, state: LoadingState.success));
  }

  _loadNews(EasyRefreshState oldState, EasyRefreshController refreshController) {
    var dataList = List.of(oldState.dataList);
    for (var i=1; i<11; i++) {
      var d = oldState.nextIndex + i;
      dataList.add(d.toString());
    }
    var nextIndex = oldState.nextIndex + 10;
    emit(oldState.copy(nextIndex: nextIndex, dataList: dataList));
    refreshController.finishLoad();
  }

  _loadOlds(EasyRefreshState oldState, EasyRefreshController refreshController) {
    var dataList = List.of(oldState.dataList);
    for (var i=1; i<11; i++) {
      var d = oldState.preIndex - i;
      dataList.insert(0, d.toString());
    }
    var nextIndex = oldState.preIndex - 10;
    emit(oldState.copy(preIndex: nextIndex, dataList: dataList));
    refreshController.finishRefresh();

    var child = BlocProvider(create: (_)=> DraggableCubit(), child: DraggableFloatingCubitView(child: Material(
      elevation: 4.0,
      borderRadius: BorderRadius.all(Radius.circular(10)),
      child: Container(
        width: 200,
        height: 100,
        padding: EdgeInsets.all(16.0),
        color: Colors.white,
        child: Text('11111111111111111111'),
      ),
    ),),);
    showOverlay(context, child);
  }
}

final class EasyRefreshState extends Equatable {

  final LoadingState state;
  final int preIndex;
  final int nextIndex;
  final List<String> dataList;

  const EasyRefreshState._({
    this.state = LoadingState.loading,
    this.preIndex = 1000,
    this.nextIndex = 1000,
    this.dataList = const <String>[],
  });

  EasyRefreshState copy({int? preIndex, int? nextIndex, List<String>? dataList, LoadingState? state}) {
    return EasyRefreshState._(
      preIndex: preIndex ?? this.preIndex,
      nextIndex: nextIndex ?? this.nextIndex,
      state: state ?? this.state,
      dataList: dataList ?? this.dataList,
    );
  }

  const EasyRefreshState.loading() : this._();

  @override
  List<Object?> get props => [state, dataList];

}

class EasyRefreshEvent {}
class EasyRefreshInitEvent extends EasyRefreshEvent {}
class EasyRefreshLoadNewsEvent extends EasyRefreshEvent {
  final EasyRefreshController refreshController;
  EasyRefreshLoadNewsEvent(this.refreshController);
}
class EasyRefreshLoadOldsEvent extends EasyRefreshEvent {
  final EasyRefreshController refreshController;
  EasyRefreshLoadOldsEvent(this.refreshController);
}